// pages/moment-detail/moment-detail.js
Page({
  data: {
    moments: [], // 改为数组存储多个朋友圈
    showCommentInput: false,
    commentText: '',
    currentMomentIndex: -1 // 当前操作的动态索引
  },

  onLoad: function(options) {
    // 生成多个朋友圈内容
    this.generateMultipleMoments();
  },

  // 生成多个朋友圈内容
  generateMultipleMoments: function() {
    const moments = [];
    const momentCount = 5; // 生成5个朋友圈
    
    for (let i = 0; i < momentCount; i++) {
      moments.push(this.generateSingleMoment(i));
    }
    
    this.setData({
      moments: moments
    });
  },

  // 生成单个朋友圈内容
  generateSingleMoment: function(index) {
    // 随机图片数量（1-9张）
    const imageCount = Math.floor(Math.random() * 9) + 1;
    
    // 生成图片URL
    const images = [];
    for (let i = 0; i < imageCount; i++) {
      images.push(`https://picsum.photos/500/500?random=${index * 10 + i}`);
    }

    // 随机文案
    const descriptions = [
      "这片风景让我感受到了大自然的宁静与美好，仿佛所有的烦恼都随风而逝。",
      "站在这里，我感受到了大自然的壮丽与神秘，每一处风景都像是一幅精心绘制的画卷。",
      "眼前的景色让我想起了那句诗：山水有相逢，终会到漓江。这里的山水美景令人流连忘返。",
      "大自然是最伟大的艺术家，创造出如此令人惊叹的景色，让人心生敬畏。",
      "每一次旅行都是一次心灵的洗礼，这片美景让我重新认识了生活的美好。",
      "站在这片风景前，时间仿佛静止了，所有的喧嚣都远离，只剩下内心的宁静。",
      "这里的风景如诗如画，让人忍不住想要永远停留在这个美好的瞬间。",
      "大自然总能给我们带来惊喜，这片美景就是最好的证明，让人感叹造物主的神奇。"
    ];
    
    // 随机用户
    const users = [
      { name: '旅行者', avatar: '/assets/images/menu/search.png' },
      { name: '摄影师小王', avatar: '/assets/images/menu/search.png' },
      { name: '户外达人', avatar: '/assets/images/menu/search.png' },
      { name: '风景爱好者', avatar: '/assets/images/menu/search.png' },
      { name: '背包客', avatar: '/assets/images/menu/search.png' }
    ];
    
    // 随机时间
    const times = ['2小时前', '5小时前', '昨天', '2天前', '3天前'];
    
    // 随机点赞用户池
    const allUsers = ['小王', '小李', '小张', '小刘', '小陈', '小杨', '小赵', '小钱'];
    
    // 随机选择点赞用户（2-5个）
    const likeCount = Math.floor(Math.random() * 4) + 2;
    const likes = [];
    for (let i = 0; i < likeCount; i++) {
      const randomUser = allUsers[Math.floor(Math.random() * allUsers.length)];
      if (!likes.includes(randomUser)) {
        likes.push(randomUser);
      }
    }
    
    // 随机评论
    const commentTemplates = [
      { user: '小王', content: '这地方真美，下次一起去！' },
      { user: '小李', content: '照片拍得真好！' },
      { user: '小张', content: '这是哪里呀？求地址！' },
      { user: '小刘', content: '太美了，已设为壁纸！' },
      { user: '小陈', content: '风景如画，人间仙境！' }
    ];
    
    // 随机选择评论（1-3条）
    const commentCount = Math.floor(Math.random() * 3) + 1;
    const comments = [];
    for (let i = 0; i < commentCount; i++) {
      const randomComment = commentTemplates[Math.floor(Math.random() * commentTemplates.length)];
      if (!comments.some(c => c.user === randomComment.user)) {
        comments.push({...randomComment});
      }
    }

    return {
      userInfo: users[index % users.length],
      content: descriptions[Math.floor(Math.random() * descriptions.length)],
      time: times[index % times.length],
      images: images,
      likes: likes,
      comments: comments,
      isLiked: false
    };
  },

  // 返回上一页
  goBack: function() {
    wx.navigateBack();
  },

  // 预览图片
  previewImage: function(e) {
    const index = e.currentTarget.dataset.index;
    const momentIndex = e.currentTarget.dataset.momentindex;
    const images = this.data.moments[momentIndex].images;
    
    wx.previewImage({
      urls: images,
      current: images[index]
    });
  },

  // 点赞/取消点赞
  toggleLike: function(e) {
    const momentIndex = e.currentTarget.dataset.index;
    const moments = this.data.moments;
    let moment = moments[momentIndex];
    
    let { likes, isLiked } = moment;
    
    if (isLiked) {
      // 取消点赞
      likes = likes.filter(user => user !== '我');
    } else {
      // 点赞
      if (!likes.includes('我')) {
        likes.unshift('我');
      }
    }
    
    // 更新数据
    const updateKey = `moments[${momentIndex}]`;
    this.setData({
      [updateKey]: {
        ...moment,
        likes: likes,
        isLiked: !isLiked
      }
    });
  },

  // 显示评论输入框
  showCommentInput: function(e) {
    const momentIndex = e.currentTarget.dataset.index;
    
    this.setData({
      showCommentInput: true,
      currentMomentIndex: momentIndex
    });
  },

  // 隐藏评论输入框
  hideCommentInput: function() {
    this.setData({
      showCommentInput: false,
      commentText: '',
      currentMomentIndex: -1
    });
  },

  // 评论输入
  onCommentInput: function(e) {
    this.setData({
      commentText: e.detail.value
    });
  },

  // 发送评论
  sendComment: function() {
    if (!this.data.commentText.trim()) return;
    
    const { moments, commentText, currentMomentIndex } = this.data;
    
    if (currentMomentIndex === -1) return;
    
    const newComment = {
      user: '我',
      content: commentText
    };
    
    // 更新数据
    const updateKey = `moments[${currentMomentIndex}].comments`;
    this.setData({
      [updateKey]: [...moments[currentMomentIndex].comments, newComment],
      commentText: '',
      showCommentInput: false,
      currentMomentIndex: -1
    });
  }
})